package arquitetura.view;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import util.DocumentoCaixaAlta;
import util.Layouts;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import javax.swing.border.LineBorder;

import exceptions.GeralException;

import arquitetura.fachada.Fachada;

import java.awt.Font;

public class Login extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8693509643744200092L;
	private JTextField txtLogin;
	private JPasswordField pwdSenha;
	private Fachada fachada;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		Layouts.layoutWindows();		
		
		try {
			Login dialog = new Login();
			dialog.setLocationRelativeTo(null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setModal(true);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Login() {
		fachada = Fachada.getInstancia();
		setBounds(100, 100, 262, 202);
		getContentPane().setLayout(null);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(new Color(240, 248, 255));
			buttonPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			buttonPane.setBounds(10, 11, 226, 115);
			getContentPane().add(buttonPane);
			buttonPane.setLayout(null);
			
			JPanel panel = new JPanel();
			panel.setBackground(new Color(255, 255, 255));
			panel.setBorder(new LineBorder(new Color(176, 224, 230)));
			panel.setBounds(10, 11, 206, 94);
			buttonPane.add(panel);
			panel.setLayout(null);
			
			JLabel label = new JLabel("Login:");
			label.setFont(new Font("Tahoma", Font.PLAIN, 11));
			label.setForeground(Color.DARK_GRAY);
			label.setBounds(14, 20, 33, 16);
			panel.add(label);
			
			txtLogin = new JTextField();
			txtLogin.setBounds(63, 17, 130, 20);
			txtLogin.setColumns(10);
			txtLogin.setDocument(new DocumentoCaixaAlta(20));
			panel.add(txtLogin);
			
			JLabel label_1 = new JLabel("Senha:");
			label_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
			label_1.setForeground(Color.DARK_GRAY);
			label_1.setBounds(14, 57, 35, 16);
			panel.add(label_1);
			
			pwdSenha = new JPasswordField();
			pwdSenha.setBounds(63, 54, 130, 20);			
			panel.add(pwdSenha);
		}
		{
			JButton okButton = new JButton("Efetuar Login");
			okButton.setBounds(20, 134, 113, 23);
			getContentPane().add(okButton);
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						String login = txtLogin.getText();
						String senha = String.valueOf(pwdSenha.getPassword());
						
						if (fachada.logouComSucesso(login, senha)) {
							int codigoUsuarioLogado = fachada.pegarCodigoUsuarioLogado(login);
							dispose();						
							Principal.getInstancia(codigoUsuarioLogado);
						} else
							txtLogin.requestFocusInWindow();
					} catch (GeralException e1) {
						JOptionPane.showMessageDialog(null, "Ocorreu o seguinte erro: " + e1.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
					}
				}
			});
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			JButton cancelButton = new JButton("Sair");
			cancelButton.setBounds(158, 134, 65, 23);
			getContentPane().add(cancelButton);
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					System.exit(0);		
				}
			});
			cancelButton.setActionCommand("Cancel");
		}
	}
}
